package edu.neu.swc.commerce.service.communication;

import feign.Logger;
import feign.Request;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * <h1>Open Feign 配置类</h1>
 */
@Configuration
public class FeignConfig {

    /**
     * <h2>开启OpenFeign日志</h2>
     */
    @Bean
    public Logger.Level feignLogger(){
        return Logger.Level.FULL; // 需要注意，工程的日志级别需要修改成Debug级别
    }

    /**
     * <h2>OpenFeign 开启重试</h2>
     * period设置为100 发起当前请求的时间间隔，单位是ms
     * maxPeriod = 1000 发起请求的最大时间间隔，单位是ms
     * maxAttempts = 5 最多请求次数
     */
    @Bean
    public Retryer feignRetryer(){
        return new Retryer.Default(
                100,
                TimeUnit.SECONDS.toMillis(1),
                5
        );
    }

    public static final int CONNECT_TIME_OUT = 5000;

    public static final int READ_TIMEOUT_MILLS = 5000;

    @Bean
    public Request.Options options(){
        return new Request.Options(
                CONNECT_TIME_OUT,TimeUnit.MICROSECONDS,
                READ_TIMEOUT_MILLS, TimeUnit.MILLISECONDS,
                true
        );
    }
}
